package cn.school.controller.workflow;

import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.exception.ApiException;
import cn.school.workflow.service.VisitApplyService;
import cn.school.workflow.vo.VisitApplyVO;
import cn.school.workflow.vo.WorkFlowVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/visitApply")
@Api(tags = "访客申请流程接口")
public class VisitApplyController {

    @Autowired
    private VisitApplyService visitApplyService;

    /**
     * 查询流程部署信息
     * @return
     * @throws ApiException
     */
    @PostMapping("/findVisitApplyPageList")
    @ApiOperation("list => 分页查询违纪申请单")
    public CommonResult<CommonPage<VisitApplyVO>> findVisitApplyPageList(@RequestBody VisitApplyVO vo) throws ApiException{
        IPage<VisitApplyVO> pageList =  visitApplyService.findVisitApplyPageList(vo);
        return  CommonResult.success(CommonPage.restPage(pageList));
    }

    @PostMapping("/cancelVisitApply")
    @ApiOperation("cancel => 撤销违纪申请")
    public CommonResult<CommonPage<VisitApplyVO>> cancelVisitApply(@RequestBody WorkFlowVO vo) throws ApiException{
        boolean flag =  visitApplyService.cancelVisitApply(vo);
        if(flag){
            return CommonResult.success();
        }
        return  CommonResult.failed("流程取消失败");
    }

    @PostMapping("/createVisitApply")
    @ApiOperation("create => 创建违纪流程")
    public CommonResult<Integer> createVisitApply(@RequestBody VisitApplyVO  vo) throws ApiException{
        if(vo.getVisitorInfoList()==null || vo.getVisitorInfoList().size()==0){
            return CommonResult.failed("缺少来访人");
        }
        int flag =  visitApplyService.createVisitApply(vo);
        if(flag>0){
            return CommonResult.success();
        }
        return  CommonResult.failed("流程创建失败");
    }

    @GetMapping("/findVisitApplyDetail/{processInstanceId}")
    @ApiOperation("detail => 违纪详情")
    public CommonResult<VisitApplyVO> findVisitApplyDetail(@PathVariable("processInstanceId") Long processInstanceId) throws ApiException{
        VisitApplyVO visitApplyVO =  visitApplyService.findVisitApplyDetail(processInstanceId);
        if(visitApplyVO!=null){
            return CommonResult.success(visitApplyVO);
        }
        return  CommonResult.failed("查询不单申请单");
    }

}
